package com.skychyn.draw;

import java.util.ArrayList;

import android.database.Cursor;

import com.skychyn.database.SolarDBAdapter;

public class DataHandler {

	ArrayList<DataEntry> entries;
	SolarDBAdapter dbAdapter;
	Cursor solarDatas;

	public double[] temperature;
	public double[] pressure;
	public String[] dates;

	public DataHandler(SolarDBAdapter dbAdapter) {
		super();
		this.dbAdapter = dbAdapter;
		entries = new ArrayList<DataEntry>();
	}

	/**
	 * @return the entries
	 */
	public ArrayList<DataEntry> getEntries() {
		return entries;
	}

	/**
	 * @param entries
	 *            the entries to set
	 */
	public void setEntries(ArrayList<DataEntry> entries) {
		this.entries = entries;
	}

	public void prepareData() {
		solarDatas = dbAdapter.fetchAllSolarData();
		

		// Get all data from solarDatas
		if (solarDatas.moveToFirst()) {
			do {
				String date = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_DATE));

				String press = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_PRES));

				String temp = solarDatas.getString(solarDatas
						.getColumnIndexOrThrow(SolarDBAdapter.KEY_TEMP));

				entries.add(new DataEntry(date, press, temp));

			} while (solarDatas.moveToNext());
		}
		solarDatas.close();
		
		fillVectors();
	}

	private void fillVectors() {
		int count = entries.size();
		temperature = new double[count];
		pressure = new double[count];
		dates = new String[count];
		
		

		for (int i = 0; i < count; i++) {
			temperature[i] = entries.get(i).temperature;
			pressure[i] = entries.get(i).pressure;
			dates[i]=entries.get(i).date.toString();					
		}
	}

}
